package com.china08.yxyapi.api.manage.tec.teaching.scoreport.stureport;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamStuReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStuReportRepository;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 学生考试报告————删除考试报告
 *
 */
@CrossOrigin
@RestController
public class ExamStuReportDelController extends YxyApiBaseController {

    @Autowired
    private ExamRepository examRepository;

    @Autowired
    private ExamStuReportRepository examStuReportRepository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/stureport/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model)
            throws ParseException {

        model.check();
        String userId = getProfile(request, response).getId();

        Exam exam = examRepository.findOne(model.id);
        List<ExamStuReport> esrLst = examStuReportRepository.findByExamId(exam.getId());
        examStuReportRepository.delete(esrLst);
        exam.setThankUsers(null);
        exam.setStatus(1);
        exam.setSendDate(null);
        examRepository.save(exam);
        // 追加积分
        yxUserPointsUtilV3.delReport(userId);
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 考试id
        public String id;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "考试编号不能为空");
        }
    }
}
